class Console_interface

  attr_reader :database, :user

  def initialize
    puts "Wellcome to Flickasa!"
    get_command
  end

  def get_command
    command = gets.chomp
    registration if command == "registration"
    login if command == "login"
    help if command == "help"
    create_gallery if command == "create gallery"
    list_galleries if command == "galleries"
    list_photos if command == "photos"
    photo_info  if command == "photo info"
    upload_photo if command == "upload"
    open_photo if command == "open"
    list_comments if command == "comments"
    list_users if command == "users"
    vote if command == "vote"
    image_rating if command == "rating"
    comment if command == "comment"
    $database.print_all_data if command == "print all"
    save if command == "save"
    exit if command == "exit"
    get_command
  end

  def help
    puts "Available commands: registration, login, exit, create gallery, galleries, photos, open, photo info, save, comment, comments, users, rating, vote, upload"
  end

  def registration
    puts "\r\nUsername:"
    username = gets.chomp
    puts "E-mail"
    email = gets.chomp
    puts "Password"
    password = gets.chomp
    puts "Repeat password:"
    password_r = gets.chomp

    if password == password_r
      if $database.add_user(User.new(username, password, email)) > 0
        puts "Registration complete."
      else
        puts "User already exist or e-mail is not valid."
      end
    else
      puts "Passowrds doesn't match."
    end
  end

  def login
    puts "\r\nUsername:"
    username = gets.chomp
    puts "Password:"
    password = gets.chomp

    if $database.user_authenticate(username, password).class == User
      @user = $database.user_authenticate(username, password)
      puts "Logged in as #{@user.username}."
    else
      puts "Wrong details."
    end
  end

  def is_logged_in
    if !defined? @user
      puts "You need to login in."
      return false
    end
    return true
  end

  def create_gallery
    return if !is_logged_in
    
    puts "\r\ntitle:"
    title = gets.chomp
    puts "description"
    description = gets.chomp
    
    $database.insert Gallery.new(@user, title, description)
  end

  def list_galleries
    puts "User id:"
    user_id = gets.chomp.to_i
    c = 0;
    $database.galleries.each {|gallery|
      if gallery.user.id == user_id
        puts "id:#{gallery.id} #{gallery.title}"
        c+=1
      end
    }
    puts "User does not have any galleries." if c == 0
    return user_id
  end

  def list_photos
    username = list_galleries
    puts "Gallery id:"
    gallery_id = gets.chomp.to_i
    c = 0;
    $database.images.each {|image|
      if image.gallery.id == gallery_id
        puts "id:#{image.id} #{image.title}"
        c+=1
      end
    }
    puts "Gallery does not contain any photos." if c == 0
    return gallery_id
  end

  def upload_photo
    return if !is_logged_in

    c = 0;
    $database.galleries.each {|gallery|
      if gallery.user == @user
        puts "id:#{gallery.id} #{gallery.title}"
        c+=1
      end
    }

    if c == 0
      puts "You not have any galleries."
      return
    end

    puts "Gallery id:"
    gallery_id = gets.chomp.to_i

    g_gallery = nil
    $database.galleries.each {|gallery|
      g_gallery = gallery if gallery.id == gallery_id
    }

    puts "Photo address:"
    photo_url = gets.chomp

    puts "Title:"
    title = gets.chomp

    puts "Description"
    description = gets.chomp

    new_img = Image.new(@user, g_gallery, title, description)
#    puts %{cmd /c "copy #{photo_url} #{PHOTO_PATH}\\#{new_img.id}.jpg"}
    $database.insert new_img
    system %{cmd /c "copy #{photo_url} #{PHOTO_PATH}\\#{new_img.id}.jpg"}
    
  end

  def photo_info
    puts "Image id:"
    i_id = gets.chomp.to_i

    $database.images.each {|image|
      if image.id == i_id
        puts "Title: #{image.title}\r\nDescription: #{image.description}\r\nRating:#{image.rating}\r\n"
      end
    }

    $database.comments.each {|comment|
      if comment.image.id == i_id
        puts "#{comment.user.username} wrote: #{comment.text}"
      end
    }
  end

  def open_photo
    puts "Image id:"
    id = gets.chomp
    begin
      file_to_open = "#{PHOTO_PATH}\\#{id}.jpg"
      if File.exist? file_to_open
        system %{cmd /c "start #{file_to_open}"}
        $database.images.each {|image|
          if image.id == id.to_i
            puts "Title: #{image.title}\r\nDescription: #{image.description}\r\nRating: #{image.rating}"
          end
        }
      else
         puts "No such image id."
      end
    rescue
      puts "No such file."
    end
  end

  def vote
    return if !is_logged_in

    puts "Image id:"
    id = gets.chomp
    image_to_vote = nil
    $database.images.each {|image|
      if image.id == id.to_i
        image_to_vote = image
        puts "Title: #{image.title}\r\nDescription: #{image.description}\r\nRating:#{image.rating}"
      end
    }
    puts "+/-:"
    grade = gets.chomp

    if grade == "+"
      grade = true
    elsif grade == "-"
      grade = false
    else
      puts "+ or - only"
      return
    end

    vote = Vote.new(image_to_vote, @user, grade)
    $database.insert vote
    puts "Voted successfuly."
  end

  def image_rating
    puts "Image id:"
    image_id = gets.chomp.to_i
    
    g_image = nil
    $database.images.each {|image|
      if image.id == image_id
        g_image = image
      end
    }

    puts g_image.rating
  end

  def list_comments
    puts "Photo id:"
    photo_id = gets.chomp.to_i
    $database.comments.each {|comment|
      if comment.image.id == photo_id
        puts "#{comment.user.username} on #{comment.time_posted} wrote: #{comment.text}"
      end
    }
  end

  def comment
    return if !is_logged_in

    puts "Photo id:"
    photo_id = gets.chomp.to_i
    image_to_comment = nil
    $database.images.each {|image|
      if image.id == photo_id
        image_to_comment = image
        puts "#{image.title}\r\n#{image.description}"
      end
    }
    puts "Comment:"
    comment_text = gets.chomp
    comment = Comment.new(@user, image_to_comment, comment_text)
    $database.insert comment
    puts "Posted successfuly."
  end

  def list_users
    $database.users.each {|usr|
      puts "id:#{usr.id} #{usr.username}"
    }
  end

  def save
    $database.save_all
    puts "Data saved."
  end

end